package net.entelijan.cobean.examples.showcase.tree;

import java.util.List;

import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.testutil.CobeanTestUtil;

import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JTreeFixture;
import org.junit.Assert;
import org.junit.Test;


@SuppressWarnings("unused")
public class TreeFestTestCase {

	@Test
	public void testSelectAuthor() {
		DefaultTreePanel panel = new DefaultTreePanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
			JTreeFixture treeFix = frameFix.tree("tree");

			TreeShowcase sc = new TreeShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			treeFix.requireNoSelection();

			TreeAuthorModel value = (TreeAuthorModel) sc.getCobean().getValue();
			LitAuthor auth = ExamplesUtil.filterAuthorsWithBooks(
					value.getAuthors()).get(3);
			sc.getCobean().setSelectedValue(auth);
			pauseShort();

			treeFix.requireSelection(10);
			pauseShort();
		} finally {
			frameFix.cleanUp();
		}
	}

	@Test
	public void testListSelectAuthor() {
		DefaultTreePanel panel = new DefaultTreePanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
			JTreeFixture treeFix = frameFix.tree("tree");

			ListTreeShowcase sc = new ListTreeShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			treeFix.requireNoSelection();

			List<Object> value = sc.getCobean().getValue();
			LitAuthor auth = (LitAuthor) value.get(3);
			sc.getCobean().setSelectedValue(auth);
			pauseShort();

			treeFix.requireSelection(3);
			pauseShort();
		} finally {
			frameFix.cleanUp();
		}
	}

	@Test
	public void testSelectBook() {
		DefaultTreePanel panel = new DefaultTreePanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
			JTreeFixture treeFix = frameFix.tree("tree");

			TreeShowcase sc = new TreeShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			treeFix.requireNoSelection();

			TreeAuthorModel value = (TreeAuthorModel) sc.getCobean().getValue();
			LitAuthor auth = ExamplesUtil.filterAuthorsWithBooks(
					value.getAuthors()).get(3);
			LitBook book = auth.getBooks().get(2);
			sc.getCobean().setSelectedValue(book);
			pauseShort();

			treeFix.requireSelection(13);
			pauseShort();
		} finally {
			frameFix.cleanUp();
		}
	}

	@Test
	public void testListSelectBook() {
		DefaultTreePanel panel = new DefaultTreePanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
			JTreeFixture treeFix = frameFix.tree("tree");

			ListTreeShowcase sc = new ListTreeShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			treeFix.requireNoSelection();

			List<Object> value = sc.getCobean().getValue();
			LitAuthor auth = (LitAuthor) value.get(3);
			LitBook book = auth.getBooks().get(2);
			sc.getCobean().setSelectedValue(book);
			pauseShort();

			treeFix.requireSelection(6);
			pauseShort();
		} finally {
			frameFix.cleanUp();
		}
	}

	@Test
	public void testSelectBookFromGUI() {
		DefaultTreePanel panel = new DefaultTreePanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
			JTreeFixture treeFix = frameFix.tree("tree");

			TreeShowcase sc = new TreeShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			treeFix.requireNoSelection();

			treeFix.selectRows(0, 3);
			pauseShort();

			LitAuthor auth = (LitAuthor) sc.getCobean().getSelectedValue();
			Assert.assertEquals("Jhumpa Lahiri", auth.getName());
			pauseShort();
		} finally {
			frameFix.cleanUp();
		}
	}

	@Test
	public void testListSelectBookFromGUI() {
		DefaultTreePanel panel = new DefaultTreePanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
			JTreeFixture treeFix = frameFix.tree("tree");

			ListTreeShowcase sc = new ListTreeShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			treeFix.requireNoSelection();

			treeFix.selectRows(0, 3);
			pauseShort();

			LitAuthor auth = (LitAuthor) sc.getCobean().getSelectedValue();
			Assert.assertEquals("Arunhati Roy", auth.getName());
			pauseShort();
		} finally {
			frameFix.cleanUp();
		}
	}

	@Test
	public void testCollapse() {
		DefaultTreePanel panel = new DefaultTreePanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
			JTreeFixture treeFix = frameFix.tree("tree");

			TreeShowcase sc = new TreeShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);
			treeFix.requireNoSelection();

			TreeAuthorModel value = (TreeAuthorModel) sc.getCobean().getValue();
			LitAuthor auth = ExamplesUtil.filterAuthorsWithBooks(
					value.getAuthors()).get(3);
			LitBook book = auth.getBooks().get(2);
			sc.getCobean().setSelectedValue(book);
			pauseShort();

			treeFix.requireSelection(13);
			pauseShort();

			JButtonFixture cabutton = frameFix.button("collapseAllButton");
			cabutton.click();
			pauseShort();

			treeFix.requireNoSelection();

		} finally {
			frameFix.cleanUp();
		}
	}

	private synchronized void pauseShort() {
		try {
			wait(200);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

	private synchronized void pauseMedium() {
		try {
			wait(1000);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

	private synchronized void pauseLong() {
		try {
			wait(4000);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

}
